<?php

// 平台
class UserAddressAction extends Action {
	
	private $cLogs = null;
	private $cUserAddress 	= null;
	private $cUsers 		= null;
	
	function __construct(){

		//判断管理员登录状态
		admin_login_check();
		
		import('@.ORG.Get');
		import('@.ORG.Post');
		import('@.ORG.UserAddress');
		import('@.ORG.Users');
		import('@.ORG.Logs');
		
		$this->cUsers = new Users();
		$this->cUserAddress = new UserAddress();
		$this->cLogs = new Logs();
	}
	
	//列表
    public function index(){

    	admin_role('UserAddress:index');

        $get = get(
            array(
                array('begin_time'			,''	,'2014-02-01'),
                array('end_time'			,''	,short_date()),
                array('search'			    ,'trim'	,1),
                array('keywords'		    ,'trim'	,''),
                array('p'				    ,'intval'	,1),
            )
        );

        //开始时间
        $begin_time	= $get['begin_time'];
        //结束时间
        $end_time	= $get['end_time'];

        //条件
        $search     = $get['search'];
        //关键词
        $keywords   = $get['keywords'];

        $page   	= $get['p'];
        $pageSize 	= 30;

        //查询条件
        $address_search = C('USER_ADDRESS_SEARCH');

    	$where = array(
            'begin_time'    => $begin_time,
            'end_time'      => $end_time,
            'search'        => array('item'=>$search,'value'=>$keywords)
        );

    	$allCount = 0;
    	$address = $this->cUserAddress->lists($where,$page,$pageSize,$allCount);
       
    	
    	/**
		 +------------------------------------------------------------------------------
		 * 分页
		 +------------------------------------------------------------------------------
		 */
		//导入分页类
		import("@.ORG.Page");
		
		$pageParams = array(
            'g'		=> 'Admin',
            'begin' => $begin,
			'end'	=> $end,
			'search' => $search,
			'keywords' => $keywords
        );
		 // 实例化分页类
	  	$p = new Page($allCount,$pageSize,$pageParams);
	  	
    	// 分页显示输出
    	$pager = $p->show();

		/**
		 +------------------------------------------------------------------------------
		 * 分页
		 +------------------------------------------------------------------------------
		 */
    	
    	$this->assign(
    		array(
    			'APP_NAME'		=> APP_NAME,
    			'address'		=> $address,
    			'pager'			=> $pager,
                'address_search'   => $address_search,
                'begin_time'	=> $begin_time,
                'end_time'		=> $end_time,
                'search'        => $search,
                'keywords'      => $keywords,
    			'MainPage'		=> 'UserAddress:index'
    		)
    	);
    	$this->display(C('ADM_TEMPLATE'));
    }
    
    public function add(){

    	admin_role('UserAddress:add');    	
    	
    	$get = get(array(
    			array('user_id'		,'intval','0')
    	));

    	$user_id = $get['user_id'];
    	
    	//会员列表
    	$page   	= 1;
    	$pageSize 	= 100000;
    	
    	$where = array();
    	$allCount = 0;
    	 
    	$users = $this->cUsers->lists($where,$page,$pageSize,$allCount);
    	
    	$this->assign(
    		array(
    			'APP_NAME'	=> APP_NAME,
    			'users'		=> $users,
    			'user_id'	=> $user_id,
    			'MainPage'	=> 'UserAddress:add'
    		)
    	);
    	
    	$this->display(C('ADM_TEMPLATE'));
    }
    
    public function add_do(){
    	
    	admin_role('UserAddress:add');
    	
   		$post = post(array(
    		array('user_id'			,'intval','0'),
    		array('location'		,'trim',''),
    		array('address'			,'trim',''),
    		array('consignee'		,'trim',''),   		
    		array('mobile_phone'	,'trim',''),
    		array('disabled'		,'intval',''),
    		array('zipcode'			,'trim','')
    	));

    	$post['admin_id'] = admin_id_ajax();
    	
    	$add_result = $this->cUserAddress->add($post);
    		
		if($add_result){
			
			$this->cLogs->add('UserAddress:add','地址添加成功:编号'.$add_result);
			
			json_success('地址添加成功');
		}else{
			json_error('地址添加失败');
		}
    }
    
    
	//批量添加
    public function import_add_do(){
    	
    	admin_role('Member:add');

    	import('@.ORG.FileCsvUpload');
		import('@.ORG.UserAddressCsv');    	
    	
       	$cfcUP = new FileCsvUpload('user_address');
    	$up_result = $cfcUP->upload();
		    	
    	if(is_null($up_result)){
    		//不是上传
    		
    		redirect('/index.php/Admin/UserAddress/add',3,'请先上传文件');
    		
    	}else if($up_result == false){
    		//上传错误
    		$up_error = $cfcUP->file_error();
    		
    		redirect('/index.php/Admin/UserAddress/add',3,$up_error);
    		
    	}else{
    		$up_success	= 1;
    		
    		$cfcUP->remove_header();
    		$file_data = $cfcUP->file_data();
    		
    		//转换
    		$cUACsv = new UserAddressCsv();
    		$address = $cUACsv->array_to_data($file_data);
    	}

    	$add_count = 0;
    	$exist_count = 0;
    	foreach ($address as $i => $a){
    		    		
    		//判断是否存在 
    		$ext_address = $this->cUserAddress->exist(array(
    			'consignee' 	=> $a['consignee'], 
    			'address' 		=> $a['address'],
    			'mobile_phone' 	=> $a['mobile_phone']
    		));
    		
    		if(!$ext_address){
	    		//添加入库
	    		$add_id = $this->cUserAddress->add($a);
	    		if($add_id > 0){
	    			$add_count++;
	    		}
    		}else{
    			$exist_count++;
    		}
    	}
    	
    	$ext_msg = '';
    	if($exist_count > 0){
    		$ext_msg = $exist_count.'条数据已存在';
    	}
    	
    	$this->cLogs->add('UserAddress:add','地址添加成功:共'.$add_count.'条数据,'.$ext_msg);
    	
    	redirect('/index.php/Admin/UserAddress/index',3,'共'.$add_count.'条数据,添加成功!'.$ext_msg);
    }
    
    
    
    
    //批量添加
    public function import_update_do(){
    	
    	admin_role('Member:add');

    	import('@.ORG.FileCsvUpload');
		import('@.ORG.UserAddressCsv');    	
    	
       	$cfcUP = new FileCsvUpload('user_address');
    	$up_result = $cfcUP->upload();
		    	
    	if(is_null($up_result)){
    		//不是上传
    		
    		redirect('/index.php/Admin/UserAddress/add',3,'请先上传文件');
    		
    	}else if($up_result == false){
    		//上传错误
    		$up_error = $cfcUP->file_error();
    		
    		redirect('/index.php/Admin/UserAddress/add',3,$up_error);
    		
    	}else{
    		$up_success	= 1;
    		
    		$cfcUP->remove_header();
    		$file_data = $cfcUP->file_data();
    		
    		//转换
    		$cUACsv = new UserAddressCsv();
    		$address = $cUACsv->update_array_to_data($file_data);
    	}

    	$all_count 		= count($address);
    	$update_count 	= 0;
    	$exist_count	= 0;
    	
    	foreach ($address as $i => $a){    		
    		$id = $a['id'];
    		
    		//判断是否存在
    		$ext_address = $this->cUserAddress->exist(array(
    			'consignee' 	=> $a['consignee'], 
    			'address' 		=> $a['address'],
    			'mobile_phone' 	=> $a['mobile_phone']
    		));
    		
    		if($ext_address && $ext_address['id'] != $id){
    			$exist_count++;
    			continue;
    		}    		
    		
    		//添加入库
    		unset($a['id']);
    		$aft_row = $this->cUserAddress->update($id,$a);
    		if($aft_row > 0){
    			$update_count++;
    		}
    	}
    	
    	$exist_msg = '';
    	if($exist_count){
    		$exist_msg = ",{$exist_count}条相同数据未更新!";
    	}
    	
    	$this->cLogs->add('UserAddress:edit','地址更新成功！共'.$all_count.'条数据,更新:'.$update_count.'条数据'.$exist_msg);
    	
    	redirect('/index.php/Admin/UserAddress/index',1,'共'.$update_count.'条数据,更新成功!'.$exist_msg);
    }
    
    
    
    //批量添加
    public function batch_add_do(){
    	
    	admin_role('UserAddress:add');
    	
    	import('@.ORG.BatchAdd');
    	import('@.ORG.UserAddressBatchAdd');
    	

   		$post = post(array(
    		array('content'	,'trim','')
    	));

    	
    	$post['admin_id'] 	= admin_id_ajax();
    	$content			= $post['content'];
    	
    	
    	$cUABatchAdd = new UserAddressBatchAdd();
    	$aly_result = $cUABatchAdd->analyze($content);
    	
    	if($aly_result){
    		//正常
    		$check_rlt = $cUABatchAdd->format_check();
    		
    		if($check_rlt){
    			//正确
    			
    			//格式化
    			$address = $cUABatchAdd->line_format();
    			
    			$add_count = 0;
    			foreach ($address as $i => $a){
    				//添加入库
    				
    				$add_id = $this->cUserAddress->add($a);
    				if($add_id > 0){
    					$add_count++;
    				}
    			}
    			
    			$this->cLogs->add('UserAddress:add','地址添加成功:共'.$add_count.'条数据');
    			
    			json_success('共'.$add_count.'条数据,添加成功!');
    		}else{
    			//有错
    			json_error('数据格式检查有错误',$cUABatchAdd->format_errors);
    		}
  
    	}else{
    		//错误 
    		json_error($cUABatchAdd->error,$cUABatchAdd->format_errors);
    	}
    }
    
    
    //详情或修改
    public  function detail(){
    	
    	admin_role('UserAddress:edit');
    	
    	$get = get(array(
    		array('id','intval',0)
    		
    	));

    	$id = $get['id'];
    	
    	//地址
    	$address = $this->cUserAddress->get_by_id($id);

    	$user_id = $address['user_id'];
    	 
    	//会员列表
    	$page   	= 1;
    	$pageSize 	= 100000;
    	 
    	$where = array();
    	$allCount = 0;
    	
    	$users = $this->cUsers->lists($where,$page,$pageSize,$allCount);
    	
    	$this->assign(
    		array(
    			'APP_NAME'		=> APP_NAME,
    			'address'		=> $address,
    			'users'			=> $users,
    			'MainPage'		=> 'UserAddress:detail'
    		)
    	);
    	
    	$this->display(C('ADM_TEMPLATE'));
    }
    
    public function update(){
    	
    	admin_role('UserAddress:edit');
    	 
		$post = post(array(
    		array('id'				,'intval','0'),
    		array('user_id'			,'intval','0'),
    		array('location'		,'trim',''),
    		array('address'			,'trim',''),
    		array('consignee'		,'trim',''),   		
    		array('mobile_phone'	,'trim',''),
    		array('disabled'		,'intval',''),
    		array('zipcode'			,'trim',''),
    		array('last_use_time'	,'','')
    	));
   	
    	$id = $post['id'];
    	
    	unset($post['id']);
    	
    	$update_result = $this->cUserAddress->update($id,$post);
    	
    	if($update_result){
    		
    		$this->cLogs->add('UserAddress:edit','地址更新成功:编号'.$id);
    		
			json_success('保存成功');
		}else{
			json_error('保存失败');
		}
    }
    
    public function export_csv(){
    
    	admin_role('UserAddress:index');
    	
    	$file_name = '地址列表 '.date('Y-m-d').'.csv';
    	
    	//导出的订单
    	$data = $this->cUserAddress->export_address();

    	export_csv($data,$file_name);
    }
}